package com.jbp.utils;

import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * UUID 生成类
 * 
 * @author zhou.liu
 * @since 2017-04-01
 */
@Component
@Configuration
public class UUIDUtil {

	private static UUIDUtil uuidUtil;

	private static Random random;


	public static UUIDUtil uuidUtil() {
		if (uuidUtil != null) {
			return uuidUtil;
		}
		return new UUIDUtil();
	}

	/**
	 * 创建 32 位 随机数
	 *
	 * @return 随机数
	 */
	public static String creatUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	public static Random getRandom() {
		if (random != null) {
			return random;
		}
		random = new Random();
		return random;
	}

	public static String getOrderIdByUUId() {
		int machineId = 1;//最大支持1-9个集群机器部署
		int hashCodeV = UUID.randomUUID().toString().hashCode();
		if (hashCodeV < 0) {//有可能是负数
			hashCodeV = -hashCodeV;
		}
		// 0 代表前面补充0
		// 4 代表长度为4
		// d 代表参数为正数型
		return machineId + String.format("%015d", hashCodeV);
	}


	public static void main(String[] args) {
		for (int i = 0; i <300 ; i++) {
			System.out.println(getNonce(999999,100000));
		}

	}


	public static BigDecimal getNonce(BigDecimal max, BigDecimal min) {


		BigDecimal db = new BigDecimal(Math.random() * Double.parseDouble(max.subtract(min).toString()) + Double.parseDouble(min.toString()));
		//System.out.println(db.setScale(3, BigDecimal.ROUND_HALF_UP)// 保留30位小数并四舍五入
		//.toString());
		return db.setScale(3, BigDecimal.ROUND_HALF_UP);
	}

	/**
	 * 创建一个随机数
	 */
	public static String getNumber(int length) {
		//随机生成数字，并添加到字符串
		String s = "";

		for (int i = 0; i < length; i++) {
			s += getRandom().nextInt(10);
		}
		return s;
	}

	/**
	 * 计算时间差
	 *
	 * @return
	 */

	public static long countSecond(Date start, Date end) {
		long interval = (end.getTime() - start.getTime()) / 1000;
		System.out.println("两个时间相差" + interval + "秒");//会打印出相差3秒
		return interval;
	}


	/**
	 * 添加指定范围随机数
	 */
	public static Integer getNonce(int max, int min) {


		return getRandom().nextInt(max) % (max - min + 1) + min;
	}

	public static double getNonce(int max, double min) {


		return getRandom().nextInt(max) % (max - min + 1) + min;
	}

	public static String inviteCode() {
		String str = "";
		for (int i = 0; i < 6; i++) {
			int num = getNonce(25, 0);
			str += Constant.LETTER.charAt(num);
		}


		return str;
	}

	public static Date getDayTime(int index) {
		TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
		TimeZone.setDefault(tz);
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		calendar.set(Calendar.DATE, index);
		String date = fmt.format(calendar.getTime());
		return calendar.getTime();
	}


	public static Date timestamp2Date(String str_num, String format ) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (str_num.length() == 13) {
			String date = sdf.format(new Date(Long.parseLong(str_num)));
			return sdf.parse(date);
		} else {
			String date = sdf.format(new Date(Integer.parseInt(str_num) * 1000L));
			return sdf.parse(date);
		}
	}


	/**
	 * 判断时间区间
	 * @param sourceTime 区间
	 * @param curTime 时间
	 * @return
	 */

	public static boolean isInTime(String sourceTime, String curTime) {
		if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
			throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
		}
		if (curTime == null || !curTime.contains(":")) {
			throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
		}
		String[] args = sourceTime.split("-");
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		try {
			long now = sdf.parse(curTime).getTime();
			long start = sdf.parse(args[0]).getTime();
			long end = sdf.parse(args[1]).getTime();
			if (args[1].equals("00:00")) {
				args[1] = "24:00";
			}
			if (end < start) {
				if (now >= end && now < start) {
					return false;
				} else {
					return true;
				}
			} else {
				if (now >= start && now < end) {
					return true;
				} else {
					return false;
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
			throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
		}

	}


}
